import { defineStore } from 'pinia'
import {
  uploadAvatarRequest,
  updateUserInfo
} from '@/service/blog/information'
import { localCache } from '@/utils/cache'
import useLoginStore from '@/store/login/login'
import { ElMessage } from 'element-plus'
import router from '@/router/index'

const loginStore  = useLoginStore()

const useInformationStore = defineStore('information', {
  state: () => ({
    
  }),
  actions: {
    async updateUserInfoAction(nickname: string, link: string, signature: string, avatarData: any) {
      updateUserInfo(nickname,link,signature).then(res => {
        // console.log(res)
        const name = localCache.getCache('name')
        const password = localCache.getCache('password')
          if(res.code == 200) {
            if( avatarData !== null && avatarData !== undefined) {
              uploadAvatarRequest(avatarData).then(res => {
                // console.log(res)
                loginStore.loginRequestAction(name,password)
                ElMessage({type: 'success', message: res.msg})
              })
            }else {
              ElMessage({type: 'success', message: res.msg})
              loginStore.loginRequestAction(name,password)
            }
          }else {
            ElMessage({type: 'success', message: res.response.data})
          }
        
        
      })
    }
  }
})

export default useInformationStore
